﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Comercio.Models.DBM;

namespace Comercio.Controllers
{
    public class detalleController : Controller
    {
        private ECommerceEntities1 db = new ECommerceEntities1();

        //
        // GET: /detalle/

        public ActionResult Index()
        {
            var tb_detalle = db.tb_detalle.Include(t => t.tb_producto).Include(t => t.tb_venta);
            return View(tb_detalle.ToList());
        }

        //
        // GET: /detalle/Details/5

        public ActionResult Details(int id = 0)
        {
            tb_detalle tb_detalle = db.tb_detalle.Find(id);
            if (tb_detalle == null)
            {
                return HttpNotFound();
            }
            return View(tb_detalle);
        }

        //
        // GET: /detalle/Create

        public ActionResult Create()
        {
            ViewBag.id_producto = new SelectList(db.tb_producto, "id_producto", "descripcion");
            ViewBag.id_venta = new SelectList(db.tb_venta, "id_venta", "rut_cliente");
            return View();
        }

        //
        // POST: /detalle/Create

        [HttpPost]
        public ActionResult Create(tb_detalle tb_detalle)
        {
            if (ModelState.IsValid)
            {
                db.tb_detalle.Add(tb_detalle);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.id_producto = new SelectList(db.tb_producto, "id_producto", "descripcion", tb_detalle.id_producto);
            ViewBag.id_venta = new SelectList(db.tb_venta, "id_venta", "rut_cliente", tb_detalle.id_venta);
            return View(tb_detalle);
        }

        //
        // GET: /detalle/Edit/5

        public ActionResult Edit(int id = 0)
        {
            tb_detalle tb_detalle = db.tb_detalle.Find(id);
            if (tb_detalle == null)
            {
                return HttpNotFound();
            }
            ViewBag.id_producto = new SelectList(db.tb_producto, "id_producto", "descripcion", tb_detalle.id_producto);
            ViewBag.id_venta = new SelectList(db.tb_venta, "id_venta", "rut_cliente", tb_detalle.id_venta);
            return View(tb_detalle);
        }

        //
        // POST: /detalle/Edit/5

        [HttpPost]
        public ActionResult Edit(tb_detalle tb_detalle)
        {
            if (ModelState.IsValid)
            {
                db.Entry(tb_detalle).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.id_producto = new SelectList(db.tb_producto, "id_producto", "descripcion", tb_detalle.id_producto);
            ViewBag.id_venta = new SelectList(db.tb_venta, "id_venta", "rut_cliente", tb_detalle.id_venta);
            return View(tb_detalle);
        }

        //
        // GET: /detalle/Delete/5

        public ActionResult Delete(int id = 0)
        {
            tb_detalle tb_detalle = db.tb_detalle.Find(id);
            if (tb_detalle == null)
            {
                return HttpNotFound();
            }
            return View(tb_detalle);
        }

        //
        // POST: /detalle/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            tb_detalle tb_detalle = db.tb_detalle.Find(id);
            db.tb_detalle.Remove(tb_detalle);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}